package cn.doyens.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    //MetaObject  meta 元  ThreadLocal
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("insertFill--:执行了字段默认填充操作");
        //通过方法传入的metaObject 对象 为指定字段设置字段值
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());//模拟 已经获取到当前操作者的id值
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        long threadId = Thread.currentThread().getId();
        log.info("updateFill---当前操作的线程id为："+threadId);
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());//模拟 已经获取到当前操作者的id值
    }
}
